package com.net.oil.main;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.net.oil.R;
import com.net.oil.base.BaseActivity;
import com.net.oil.main.contract.FeedbackContract;
import com.net.oil.main.presenter.FeedbackPresenter;
import com.net.oil.util.StringUtil;
import com.net.oil.widget.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 意见反馈
 */
public class FeedbackActivity extends BaseActivity<FeedbackPresenter> implements FeedbackContract.View {

    @BindView(R.id.toolbar_go_back)
    ImageView toolbarGoBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.input_feedback_et)
    EditText inputFeedbackEt;
    @BindView(R.id.word_number_tv)
    TextView wordNumberTv;
    @BindView(R.id.commit_feedback_bt)
    Button commitFeedbackBt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);
        ButterKnife.bind(this);
        presenter = new FeedbackPresenter(this);
        initView();
    }

    private void initView() {
        toolbarTitle.setText("意见反馈");

        wordNumberTv.setText(String.format(getString(R.string.word_number), 200));
        inputFeedbackEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                wordNumberTv.setText(String.format(getString(R.string.word_number), 200 - start - count));
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @OnClick({R.id.toolbar_go_back, R.id.commit_feedback_bt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.toolbar_go_back:
                finish();
                break;
            case R.id.commit_feedback_bt:
                String content = inputFeedbackEt.getText().toString().trim();
                if (!StringUtil.isEmpty(content)){
                    showProgressDialog();
                    presenter.commitFeedback(content);
                }else {
                    ToastUtil.showErrorToast(this, "请输入反馈内容");
                }
                break;
        }
    }

    /**
     * 提交意见反馈成功
     */
    @Override
    public void commitFeedbackSuccess() {
        dismissProgressDialog();
        ToastUtil.showSuccessToast(this, "提交成功");
    }

    /**
     * 提交意见反馈失败
     */
    @Override
    public void commitFeedbackFailed() {
        dismissProgressDialog();
        ToastUtil.showErrorToast(this, "提交失败");
    }
}
